import { Component, OnInit } from '@angular/core';
import { HeroService } from '../../../services/hero.service';
import { chartOption_bar1, chartOption_bar2 } from '../index/echartscanvas/echartscanvas';
import { ElementRef, ViewChild } from '@angular/core';

@Component({
  selector: 'app-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss']
})
export class IndexComponent implements OnInit {

  @ViewChild('indexdiv') elementView: ElementRef;
  viewable: boolean;
  datetype: string = "1";
  dayData: Array<any> = [];
  monthData: Array<any> = [];
  yearData: Array<any> = [];
  Alldata = {
    taskSum: 0,
    taskWarningSum: 0,
    taskErrSum: 0,
    totalPrinterSum: 0,
    blackPrinterSum: 0,
    coloursPrinterSum: 0,
    taskOrderSum: 0,
    yesTaskOrderSum: 0,
    noTaskOrderSum: 0,
    billSum: 0,
    yesBillSum: 0,
    noBillSum: 0,
    userSum: 0,
    yesUserSum: 0,
    noUserSum: 0,
    printerSum: 0,
    yesPrinterSum: 0,
    noPrinterSum: 0
  };
  //柱状图
  chartOption_1;
  chartOption_2;
  chartOption_3;
  chartOption_4;
  chartOption_5;
  chartOption_6;

  constructor(private heroService: HeroService) { }

  ngOnInit() {
    this.getViewData();
  }

  // 获取全部数据
  getViewData() {
    let url = host + "/main/indexs/data";
    this.heroService.get(url).then(result => {
      if (result.code == 0) {
          if( JSON.stringify(result.data.totle) !== '{}' ){
            this.Alldata = result.data.totle;
          }
          this.dayData = result.data.day;
          this.monthData = result.data.month;
          this.yearData = result.data.year;
          this.getBardata(this.datetype);
      }
    })
  }

  //获取柱状图
  getBardata(type) {
    this.chartOption_1 = JSON.parse(JSON.stringify(chartOption_bar2));
    this.chartOption_2 = JSON.parse(JSON.stringify(chartOption_bar2));
    this.chartOption_3 = JSON.parse(JSON.stringify(chartOption_bar2));
    this.chartOption_4 = JSON.parse(JSON.stringify(chartOption_bar2));
    this.chartOption_5 = JSON.parse(JSON.stringify(chartOption_bar1));
    this.chartOption_6 = JSON.parse(JSON.stringify(chartOption_bar1));
    this.chartOption_1.legend.data.push('黑白纸张', '彩色纸张');
    this.chartOption_1.series[0].name = "黑白纸张";
    this.chartOption_1.series[1].name = "彩色纸张";

    this.chartOption_2.legend.data.push('故障', '预警');
    this.chartOption_2.series[0].name = "故障";
    this.chartOption_2.series[1].name = "预警";

    this.chartOption_3.legend.data.push('已完成工单', '新加工单');
    this.chartOption_3.series[0].name = "已完成工单";
    this.chartOption_3.series[1].name = "新加工单";

    this.chartOption_4.legend.data.push('已支付账单', '新添加账单');
    this.chartOption_4.series[0].name = "已支付账单";
    this.chartOption_4.series[1].name = "新添加账单";

    this.chartOption_5.legend.data.push('租户量');
    this.chartOption_5.series[0].name = "租户量";

    this.chartOption_6.legend.data.push('设备量');
    this.chartOption_6.series[0].name = "设备量";

    if (type == '1') {
      let data = this.yearData;
      for (let i in data) {
        this.chartOption_1.xAxis[0].data.push(data[i].dateAdd.slice(0, 4));
        this.chartOption_2.xAxis[0].data.push(data[i].dateAdd.slice(0, 4));
        this.chartOption_3.xAxis[0].data.push(data[i].dateAdd.slice(0, 4));
        this.chartOption_4.xAxis[0].data.push(data[i].dateAdd.slice(0, 4));
        this.chartOption_5.xAxis[0].data.push(data[i].dateAdd.slice(0, 4));
        this.chartOption_6.xAxis[0].data.push(data[i].dateAdd.slice(0, 4));
        this.chartOption_1.series[0].data.push(data[i].blackPrinterSum);
        this.chartOption_1.series[1].data.push(data[i].coloursPrinterSum);

        this.chartOption_2.series[0].data.push(data[i].taskErrSum);
        this.chartOption_2.series[1].data.push(data[i].taskWarningSum);

        this.chartOption_3.series[0].data.push(data[i].yesTaskOrderSum);
        this.chartOption_3.series[1].data.push(data[i].newTaskOrderSum);

        this.chartOption_4.series[0].data.push(data[i].yesBillSum);
        this.chartOption_4.series[1].data.push(data[i].newBillSum);

        this.chartOption_5.series[0].data.push(data[i].newUserSum);

        this.chartOption_6.series[0].data.push(data[i].newPrinterSum);
      }
    } else if (type == '2') {
      let data = this.monthData;
      for (let i in data) {
        this.chartOption_1.xAxis[0].data.push(data[i].dateAdd.slice(0, 7));
        this.chartOption_2.xAxis[0].data.push(data[i].dateAdd.slice(0, 7));
        this.chartOption_3.xAxis[0].data.push(data[i].dateAdd.slice(0, 7));
        this.chartOption_4.xAxis[0].data.push(data[i].dateAdd.slice(0, 7));
        this.chartOption_5.xAxis[0].data.push(data[i].dateAdd.slice(0, 7));
        this.chartOption_6.xAxis[0].data.push(data[i].dateAdd.slice(0, 7));
        this.chartOption_1.series[0].data.push(data[i].blackPrinterSum);
        this.chartOption_1.series[1].data.push(data[i].coloursPrinterSum);

        this.chartOption_2.series[0].data.push(data[i].taskErrSum);
        this.chartOption_2.series[1].data.push(data[i].taskWarningSum);

        this.chartOption_3.series[0].data.push(data[i].yesTaskOrderSum);
        this.chartOption_3.series[1].data.push(data[i].newTaskOrderSum);

        this.chartOption_4.series[0].data.push(data[i].yesBillSum);
        this.chartOption_4.series[1].data.push(data[i].newBillSum);

        this.chartOption_5.series[0].data.push(data[i].newUserSum);

        this.chartOption_6.series[0].data.push(data[i].newPrinterSum);
      }
    } else {
      let data = this.dayData;
      for (let i in data) {
        this.chartOption_1.xAxis[0].data.push(data[i].dateAdd.slice(0, 10));
        this.chartOption_2.xAxis[0].data.push(data[i].dateAdd.slice(0, 10));
        this.chartOption_3.xAxis[0].data.push(data[i].dateAdd.slice(0, 10));
        this.chartOption_4.xAxis[0].data.push(data[i].dateAdd.slice(0, 10));
        this.chartOption_5.xAxis[0].data.push(data[i].dateAdd.slice(0, 10));
        this.chartOption_6.xAxis[0].data.push(data[i].dateAdd.slice(0, 10));
        this.chartOption_1.series[0].data.push(data[i].blackPrinterSum);
        this.chartOption_1.series[1].data.push(data[i].coloursPrinterSum);

        this.chartOption_2.series[0].data.push(data[i].taskErrSum);
        this.chartOption_2.series[1].data.push(data[i].taskWarningSum);

        this.chartOption_3.series[0].data.push(data[i].yesTaskOrderSum);
        this.chartOption_3.series[1].data.push(data[i].newTaskOrderSum);

        this.chartOption_4.series[0].data.push(data[i].yesBillSum);
        this.chartOption_4.series[1].data.push(data[i].newBillSum);

        this.chartOption_5.series[0].data.push(data[i].newUserSum);

        this.chartOption_6.series[0].data.push(data[i].newPrinterSum);
      }
    }
    this.chartOption_1 = Object.assign({}, this.chartOption_1);
    this.chartOption_2 = Object.assign({}, this.chartOption_2);
    this.chartOption_3 = Object.assign({}, this.chartOption_3);
    this.chartOption_4 = Object.assign({}, this.chartOption_4);
    this.chartOption_5 = Object.assign({}, this.chartOption_5);
    this.chartOption_6 = Object.assign({}, this.chartOption_6);
  }
}